package com.session5;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

public class JComboDemo extends JFrame implements ActionListener{
	private JComboBox cboStudent;
	private JButton addItem;
	private JTextField txtItem;
	private String[] studentList = {"Pham Van An","Nguyen thi Thu","Nguyen Van Tung","Pham Van Viet"};
	private Vector<String> vtrData = null;
	public JComboDemo() {
		this.vtrData = new Vector<String>();
		this.vtrData.addElement("Pham Van Minh");
		this.vtrData.addElement("Nguyen Thi Mai");
		this.vtrData.addElement("Phan Minh Ly");
	}
	public void createForm(){
		this.setLayout(new MigLayout());
//		this.cboStudent = new JComboBox(studentList);
		this.cboStudent = new JComboBox(vtrData);
		this.addItem = new JButton("Add");
		this.txtItem = new JTextField(20);
		this.getContentPane().add(this.cboStudent,"wrap");
		this.getContentPane().add(this.addItem);
		this.getContentPane().add(this.txtItem);
		
		//Dang Ki Su Kien 
		this.addItem.addActionListener(this);
		
	}
	public void showForm(){
		this.setTitle("JCombo Demo");
		this.setSize(400,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		JComboDemo d = new JComboDemo();
		d.createForm();
		d.showForm();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == this.addItem){
//			String value = this.txtItem.getText();
//			this.vtrData.addElement(value);
			this.cboStudent.addItem(txtItem.getText());
		}
	}

}
